/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Answer;
import models.Course;
import models.Question;
import models.Test;
import org.hibernate.Session;
import service.CourseDao;
import service.HibernateUtil;
import service.ProgressTestDao;

/**
 *
 * @author kjeld
 */
//@WebServlet(name = "ProgressTestController", urlPatterns = {"/student/tests"})
public class ProgressTestController extends HttpServlet {

    HibernateUtil HU = new HibernateUtil();
    Session session = HU.openSession();
    CourseDao cdao = new CourseDao(session);
    ProgressTestDao pdao = new ProgressTestDao(session);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sessie = request.getSession(true);
        if (request.getParameter("sc") != null) {//als er een course is geselcteerd
            Long id = Long.parseLong(request.getParameter("sc"));
            Course course = cdao.get(id);
            List<Test> allTest = pdao.findAllTests(course.getCourseID());

            request.setAttribute("selectedCourse", course);
            request.setAttribute("courseTests", allTest);
            sessie.setAttribute("selectedTest", null);
            sessie.setAttribute("testQuestions", null);

            forward(request, response, "tutorprogresstest.jsp");
        } else if (request.getParameter("st") != null) {//selected test
            Long id = Long.parseLong(request.getParameter("st"));
            Test test = pdao.getTest(id);
            if(request.getParameter("type")!=null){
                pdao.deleteTest(test);
            }else{
            
            try{
                Set<Question> allQuestions = test.getQuestions();
                sessie.setAttribute("testQuestions", allQuestions);
            }catch(Exception e){
                System.out.println("No questions probably: "+e);
            }

            sessie.setAttribute("selectedTest", test);
            
            }

            forward(request, response, "tutorprogresstest.jsp");
        } else if (request.getParameter("sq") != null) {
            if (request.getParameter("type").equals("delete")) {
                Question q = pdao.getQuestion(Long.parseLong(request.getParameter("sq")));
                pdao.deleteQuestion(q, request.getParameter("id"));

                forward(request, response, "progress?st=" + request.getParameter("id"));
            } else if (request.getParameter("type").equals("answers")) {
                List<Answer> theAnswers = pdao.getAnswers(Long.parseLong(request.getParameter("sq")));
                request.setAttribute("questionDetails", theAnswers);

                forward(request, response, "tutorprogresstest.jsp");
            }
        } else {
            response.sendRedirect("tutorprogresstest.jsp");
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("type").equals("create")) {
            String name = "";
            Long courseID = Long.parseLong(request.getParameter("course"));
            name = request.getParameter("testname");
            Course course = cdao.get(courseID);

            Test newTest = new Test(name);
            newTest.setCourse(course);
            pdao.saveTest(newTest);

            forward(request, response, "tutorprogresstest.jsp");
        }
        if (request.getParameter("type").equals("add")) {
            Test test = pdao.getTest(Long.parseLong(request.getParameter("testIDD")));

            Question newQuestion = new Question();
            newQuestion.setQuestion(request.getParameter("question"));
            pdao.saveQuestion(newQuestion);
            System.out.println("lalala "+newQuestion.getQuestion());
            test.addAQuestion(newQuestion);
            pdao.updateTest(test);

            for (int i = 1; i < 4; i++) {
                Answer answer = new Answer();
                answer.setAnswer(request.getParameter("answer_" + i));
                answer.setCorrect(request.getParameter("correct_" + i));
                System.out.println(answer.getCorrect());
                answer.setParentQuestion(newQuestion);
                pdao.saveAnswer(answer);
            }
            response.sendRedirect("tutorprogresstest.jsp");

        }
    }

    protected void forward(HttpServletRequest request, HttpServletResponse response, String url)
            throws ServletException, IOException {
        String address = "/" + url;
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
